package com.example.beautymakeup.service.impl;

import com.example.beautymakeup.dao.ClassifyDao;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.pojo.Classify;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.service.ClassifyService;
import com.example.beautymakeup.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {
    @Autowired
    private ClassifyDao classifyDao;

    @Override
    public PageBean<Classify> get(String token) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            List<Classify> classifies = classifyDao.get();
            int count = classifyDao.getCount();
            PageBean<Classify> pageBean = new PageBean<>();
            pageBean.setTotalCount(count);
            pageBean.setRows(classifies);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Classify> getAll(String token, int page, int size) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            List<Classify> all = classifyDao.getAll((page - 1) * size, size);
            int allCount = classifyDao.getAllCount();
            PageBean<Classify> pageBean = new PageBean<>();
            pageBean.setRows(all);
            pageBean.setTotalCount(allCount);
            return pageBean;
        }
        return null;
    }

    @Override
    public PageBean<Classify> getTypeAll(String token) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            List<Classify> all = classifyDao.getTypeAll();
            int allCount = classifyDao.getAllCount();
            PageBean<Classify> pageBean = new PageBean<>();
            pageBean.setRows(all);
            pageBean.setTotalCount(allCount);
            return pageBean;
        }
        return null;
    }

    @Override
    public boolean update(String token, Classify classify) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return classifyDao.update(classify) == 1;
        }
        return false;
    }

    @Override
    public boolean add(String token, Classify classify) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return classifyDao.add(classify) == 1;
        }
        return false;
    }

    @Override
    public boolean ban(String token, Classify classify) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return classifyDao.ban(classify) == 1;
        }
        return false;
    }

    @Override
    public boolean delete(String token, Classify classify) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if("admin".equals(subject)){
            return classifyDao.delete(classify) == 1;
        }
        return false;
    }
}
